<?php
header("Content-Type: application/json; charset=UTF-8");
require_once 'db.php';

$user_id = isset($_GET['user_id']) ? intval($_GET['user_id']) : 0;

if ($user_id <= 0) {
    echo json_encode([
        'status' => 'error',
        'message' => '无效的用户ID'
    ]);
    exit;
}

try {
    $sql = "SELECT * FROM feedbacks 
            WHERE user_id = ? 
            ORDER BY created_at DESC";
            
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("i", $user_id);
    $stmt->execute();
    $result = $stmt->get_result();
    
    $feedbacks = [];
    while ($row = $result->fetch_assoc()) {
        // 格式化时间
        $row['created_at'] = date('Y-m-d H:i', strtotime($row['created_at']));
        if ($row['reply_time']) {
            $row['reply_time'] = date('Y-m-d H:i', strtotime($row['reply_time']));
        }
        
        // 处理图片路径
        if ($row['images']) {
            $row['images'] = explode(',', $row['images']);
        } else {
            $row['images'] = [];
        }
        
        $feedbacks[] = $row;
    }
    
    echo json_encode([
        'status' => 'success',
        'list' => $feedbacks
    ]);

} catch (Exception $e) {
    echo json_encode([
        'status' => 'error',
        'message' => $e->getMessage()
    ]);
} finally {
    if (isset($stmt)) $stmt->close();
    $conn->close();
}
?> 